import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:mobile_vpn/common/view_state/view_state_logic.dart';

import '../../../http/retrofit_client.dart';
import '../../../models/login_entity.dart';

class PasswordLoginLogic extends ViewStateLogic {
  TextEditingController emailController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  GlobalKey<FormState> formKey = GlobalKey<FormState>();
  RxString tel = ''.obs;
  RxString password = ''.obs;
  RxString phoneErrorText = ''.obs;
  RxBool isPasswordVisible = false.obs;

  String validatePhone(phone) {
    tel.value = phone;
    if (phone.isEmpty) {
      phoneErrorText.value = '请输入手机号码';
    } else if (phone.length != 11 || !RegExp(r'^\d{11}$').hasMatch(phone)) {
      phoneErrorText.value = '请输入正确的,已被实名注册的手机号码';
    } else {
      phoneErrorText.value = '';
    }
    return phoneErrorText.value;
  }

  String validatePassword(value) {
    password.value = value;
    if (value.isEmpty) {
      return '请输入密码';
    }
    return '';
  }

  /// 验证码登录
  void loginByCode() {
    sendRequest(
        RetrofitClient.instance.apiService.userLogin(tel.value, password.value),
        showLoadingDialog: true,
        bindViewState: false,
        successCallback: (value) {
          print(value);
          Get.back();
        });
  }



  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    emailController.dispose();
    passwordController.dispose();
  }
}